<?php

include "./../funcs/dbfunctions.inc";
if(!(isset($_POST['channelid']) && isset($_POST['map'])&& isset($_POST['enemy'])&& isset($_POST['lineup'])&& isset($_POST['reserve']) &&isset($_POST['fc']) && isset($_POST['pw'])))
{
	die("invalid post parameter for postbattle.php");
}

$db = new mysqli("127.0.0.1","fc","keks");
if(mysqli_connect_errno($db))
{
	die("failed to Connect to Database: ". mysqli_connect_error());
}



	
define('SALT_LENGTH', 15);
function HashMe($phrase,$salt)
{
	return hash('sha512',$salt .  $phrase);
}



$sql = "SELECT password FROM wottools.users WHERE id = ?;";
$stmt = $db->prepare($sql);
$stmt->bind_param("i",$_POST['fc']);
$stmt->execute();
$stmt->bind_result($hashedpw);
if($stmt->fetch())
{
	$stmt->close();
	if($hashedpw===HashMe($_POST['pw'],"0fd7f937c2593dbe94b019c2ad45d410"))
	{
		
		$kasse = getOrCreateUserByWoTAccountid($db,-1);


		insertWoTCWChannel($db,$_POST['channelid'],$_POST['map'],$_POST['enemy'],$_POST['fc']);



		$lineuparray = explode("|",$_POST["lineup"]);
		foreach($lineuparray as $lineupplayer)
		{
			if(!($lineupplayer===""))
			{
				$dbid = getOrCreateUserByWoTAccountid($db,$lineupplayer);
				insertCombatant($db,$dbid,$_POST['channelid'],0,$_POST["fc"]);
			}
		}


		$reservearray = explode("|",$_POST["reserve"]);	
		foreach($reservearray as $reserveplayer)
		{
			if(!($reserveplayer===""))
			{
				$dbid = getOrCreateUserByWoTAccountid($db,$reserveplayer);
				insertCombatant($db,$dbid,$_POST['channelid'],1,$_POST["fc"]);
			}
		}
	}
}
else
{
	$stmt->close();
}







?>